﻿using System;
using BruTile.Cache;
using BruTile.Web;
using BruTile;
using System.Configuration;

namespace BruTileArcGIS
{
    public enum BaiduMapType
    {
        /// <summary>
        /// 百度路线
        /// </summary>
        Roads,

        /// <summary>
        /// 百度影像
        /// </summary>
        Aerial,

        /// <summary>
        /// 百度地图
        /// </summary>
        Map
    }
    public class ConfigBaidu : IConfig
    {
        private BaiduMapType _type = BaiduMapType.Map;
        public ConfigBaidu(BaiduMapType mapType)
        {
            this._type = mapType;
        }
        public ITileSource CreateTileSource()
        {
            Configuration config = ConfigurationHelper.GetConfig();

            string bingToken = config.AppSettings.Settings["BingToken"].Value;
            string bingUrl = config.AppSettings.Settings["BingUrl"].Value;
            //BingMapType mapType = BingMapType.Hybrid;

            //string url = "http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer/tile/{0}/{2}/{1}";

            string url = "http://q3.baidu.com/it//u=x={1};y={2};z={0};v=014;type=web&fm=44";

            if (this._type == BaiduMapType.Aerial)
            {
                url = "http://q4.baidu.com/it/u=x={1};y={2};z={0};v=009;type=sate&fm=46";
            }
            else if (this._type == BaiduMapType.Roads)
            {

                url = "http://q7.baidu.com/it/u=x={1};y={2};z={0};v=014;type=trans&fm=47";
            }

            BruTile.ITileProvider provider = new BruTile.Web.WebTileProvider(new BasicRequest(url));

            

            return new BruTile.TileSource(provider, Schema);
            
        }

        private static ITileSchema Schema
        {
            get
            {
                TileSchema schema = new TileSchema();
                double[] resolutions = new double[19];
                for (int i = 0; i < 19; i++)
                {
                    resolutions[i] = Math.Pow(2, 18 - i);
                    Resolution res = new Resolution();
                    res.Id = i.ToString();
                    res.UnitsPerPixel = resolutions[i];
                    schema.Resolutions.Add(res);
                }
                schema.Height = 256;
                schema.Width = 256;
                schema.Extent = new Extent(-20037508.34, -20037508.34, 20037508.34, 20037508.34);
                schema.OriginX = 0;
                schema.OriginY = 0;
                schema.Format = "png";
                schema.Name = "BAIDU";
                schema.Axis = AxisDirection.Normal;
                schema.Srs = "EPSG:900913";

                return schema;
            }
        }
    }
}

